<?php

namespace App\Http\Controllers;

use App\Models\Activity;
use Carbon\Carbon;
use Illuminate\Http\Request;

class ActivitiesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $lists = Activity::query()
            ->where('status', 1)
            ->where('end_time', '>', Carbon::now())
            ->paginate();
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $key = 'rush_' . $id;
        $activity = \Redis::get($key);
        if (!$activity) {
            $activity = Activity::findOrFail($id);
            if ($activity->status && $activity->end_time > Carbon::now()) {
                \Redis::set($key, $activity);
            }
        }

        return redirect(route('products.show',['id' => $activity->product->id]));
    }
}
